package src.model.gameObject.ability;

import src.model.gameObject.GameObject;
import src.model.gameObject.eventBrokers.MoveEventBroker;
import src.model.gameObject.eventInterfaces.MovePositionListener;
import src.model.gameWorld.LookupTable;
import src.model.gameWorld.Position;
import src.event.events.RemoveObject;
import src.model.gameWorld.ActualMap;

/**
 * <b>Overview</b><br>
 * <p>This class represents the ability for an area effect cause
 * Instant Death. A GameObject that has this ability will cause
 * anything that moves onto its location to be instantly destroyed</p>
 * <br><br>
 * <b>Responsibilites</b><br>
 * <p>Destroy anything that moves onto it.</p>
 * <br><br>
 * <b>Collaborators</b>
 * <br>
 * MoveEventBroker: Informs it when something has moved onto it.
 * <br>
 * <b>Implementor</b> - Tristan Lohman<br>
 * <b>Tester</b> - Tim<br>
 *  
 * 
 * @author Tristan
 */
public class AEIntstantDeath extends Ability implements MovePositionListener {
	private ActualMap am;
	/**
	 * Constructs a new Area Effect, and registers it with the MoveEventBroker.
	 * @param go The GameObject owning this
	 * @param am The ActualMap that this will exist on.
	 * @param lookup The LookupTable for this Area Effect.
	 */
	public AEIntstantDeath(GameObject go, ActualMap am, LookupTable lookup) {
		super(go, AbilityType.AEINSTANTDEATH, lookup);
		MoveEventBroker.register(this.gameObject(), this);
		MoveEventBroker.register(am.findObject(go), this);
		this.am = am;
	}
	
	/**
	 * This is called by the MoveEventBroker to notify it that something has moved onto its location.
	 * It then destroys the GameObject.
	 * @param go The GameObject that moved onto this.
	 */
	public void notifyPositionMovedOnto(GameObject go){
		RemoveObject destroyer = new RemoveObject(go);
		src.model.gameWorld.GameWorldEventHandler.fireEventNow(destroyer);
	}

	/**
	 * Notifies this when its PlayerGameObject has moved, allowing it to notify the MoveEventBroker
	 * to change its location that it listens for. This allows us to have area effects that move.<br>
	 * NOTE: Better design to have MoveEventBroker handle this.
	 * @param p The Position that this Ability's Owner moved to.
	 */
	public void notifyGameObjectMoved(Position p){
		MoveEventBroker.remapListener(this, p);
	}

	public void destroySub(){
		this.am = null;
		MoveEventBroker.unregister(am.findObject(super.gameObject()), this);
		MoveEventBroker.unregister(super.gameObject(), this);
	}
}
